var close = document.getElementsByClassName("close")[0];
var dialog = document.getElementById("dialog");
var diatitle = document.getElementById("diatitle");
var diacontent = document.getElementById("diacontent");
var mask = document.getElementsByClassName("mask")[0];
var isDraging = false; //是否可拖拽的标记
diatitle.onmousedown = down;
document.onmousemove = move;
document.onmouseup = up;
var login = document.getElementById("login");
login.onclick = function () {
    dialog.style.display = "block";
}
//自动居中-登陆浮层
close.onclick = function () {
    dialog.style.display = "none";
}

function down() {
    diatitle.style.cursor = "move";
    isDraging = true;
    objleft = dialog.offsetLeft;
    objtop = dialog.offsetTop;
    posX = parseInt(mousePosition(event).x)
    posY = parseInt(mousePosition(event).y);
    offsetX = posX - objleft;
    offsetY = posY - objtop;
}
//移动
function move(event) {
    if (isDraging == true) {
        var x = mousePosition(event).x - offsetX;
        var y = mousePosition(event).y - offsetY;
        var w = document.documentElement.clientWidth - dialog.offsetWidth;
        var h = document.documentElement.clientHeight - dialog.offsetHeight;
        x = Math.min(w, Math.max(0, x));
        y = Math.min(h, Math.max(0, y));
        dialog.style.left = x + 'px';
        dialog.style.top = y + 'px';
    }
}
//松开
function up() {
    isDraging = false;
}

function mousePosition(evt) {
    var xPos, yPos;
    evt = evt || window.event;
    if (evt.pageX) {
        xPos = evt.pageX;
        yPos = evt.pageY;
    } else {
        xPos = evt.clientX + document.body.scrollLeft - document.body.clientLeft;
        yPos = evt.clientY + document.body.scrollTop - document.body.clientTop;
    }
    return {
        x: xPos,
        y: yPos
    }
}